package com.mh.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Date:2025/2/11
 * author:zmh
 * description: RabbitMQ配置
 **/

@Configuration
public class RabbitMQConfig {

    // 默认给注册用户添加默认账本
    @Bean
    public Queue addBookQueue() {
        return new Queue("addBookForNewUser", true);
    }

    // 添加账单
    @Bean
    public Queue addBillQueue() {
        return new Queue("addBill", true);
    }

    // 转账
    @Bean
    public Queue transferQueue() {
        return new Queue("transfer", true);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }
}
